package org.spruice;

import java.util.HashSet;
import java.util.Set;

/**
 * User: mrettig
 * Date: Aug 23, 2009
 */
public class CamelCaseNamer implements AutoNamer {

    private final Set<String> names = new HashSet<String>();

    public String next(Class clzz) {
        String name = format(clzz.getSimpleName());

        if (names.contains(name))
            throw new BeanExistsException(name);
        else
            names.add(name);

        return name;
    }

    public static String format(String name) {
        return name.substring(0, 1).toLowerCase() + name.substring(1);
    }

    public static class BeanExistsException extends RuntimeException {

        public BeanExistsException(String name) {
            super("Bean already exists: " + name);
        }
    }

}
